package com.ruoyi.web.controller.app;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.TRealm;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ITRealmService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TWorkOrder;
import com.ruoyi.system.service.ITWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投放Controller
 * 
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/system/workOrder")
public class TWorkOrderController extends BaseController
{
    @Autowired
    private ITWorkOrderService tWorkOrderService;

    /**
     * 查询投放列表
     */
    @PreAuthorize("@ss.hasPermi('system:workOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWorkOrder tWorkOrder)
    {
        startPage();
        if (!checkAdmin()) tWorkOrder.setUid(getUserId());
        List<TWorkOrder> list = tWorkOrderService.selectTWorkOrderList(tWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出投放列表
     */
    @PreAuthorize("@ss.hasPermi('system:workOrder:export')")
    @Log(title = "投放", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TWorkOrder tWorkOrder)
    {
        List<TWorkOrder> list = tWorkOrderService.selectTWorkOrderList(tWorkOrder);
        ExcelUtil<TWorkOrder> util = new ExcelUtil<TWorkOrder>(TWorkOrder.class);
        util.exportExcel(response, list, "投放数据");
    }

    /**
     * 获取投放详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:workOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tWorkOrderService.selectTWorkOrderById(id));
    }

    /**
     * 新增投放
     */
    @PreAuthorize("@ss.hasPermi('system:workOrder:add')")
    @Log(title = "投放", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWorkOrder tWorkOrder)
    {
        tWorkOrder.setUid(getUserId());
        return toAjax(tWorkOrderService.insertTWorkOrder(tWorkOrder));
    }

    /**
     * 修改投放
     */
    @PreAuthorize("@ss.hasPermi('system:workOrder:edit')")
    @Log(title = "投放", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWorkOrder tWorkOrder)
    {
        return toAjax(tWorkOrderService.updateTWorkOrder(tWorkOrder));
    }

    /**
     * 删除投放
     */
    @PreAuthorize("@ss.hasPermi('system:workOrder:remove')")
    @Log(title = "投放", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tWorkOrderService.deleteTWorkOrderByIds(ids));
    }

    @Autowired
    private ITRealmService itRealmService;

    @GetMapping("/getRealmList")
    public AjaxResult getRealmList(){
        return AjaxResult.success(itRealmService.selectTRealmList(new TRealm()));
    }

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 获取投手信息
     */
    @GetMapping("/getPeoples")
    public AjaxResult getPeoples(){

        SysUser sysUser = new SysUser();
        if (!checkAdmin()){
            sysUser.setUserId(getUserId());
        }
        return AjaxResult.success(sysUserMapper.selectUserList(sysUser));
    }
}
